package org.openstack.service.meter;

import org.openstack.api.meter.MeterService;
import org.openstack4j.model.telemetry.Meter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiyun on 2017/8/28.
 */
public class MeterServiceImpl implements MeterService {

    org.openstack4j.api.telemetry.MeterService service
            = new org.openstack4j.openstack.telemetry.internal.MeterServiceImpl();

    /**
     * 获取所有监控指标
     *
     * @return 监控指标列表
     */
    @Override
    public List<? extends Meter> getMeterList() {
        return service.list();
    }

    /**
     * 通过监控指标名称获取所有监控指标
     * TODO: 优化查询性能
     *
     * @param meterName 监控指标名称
     * @return 监控指标列表
     */
    @Override
    public List<? extends Meter> getMeterListByName(String meterName) {
        List<Meter> meters = new ArrayList<>();
        for (Meter m : getMeterList()) {
            if (m.getName().equals(meterName)) {
                meters.add(m);
            }
        }
        return meters;
    }

    /**
     * 通过资源ID获取对应的监控指标列表
     * TODO: 优化查询性能
     *
     * @param resourceId 资源ID
     * @return 监控指标列表
     */
    @Override
    public List<? extends Meter> getMeterListByResourceId(String resourceId) {
        List<Meter> meters = new ArrayList<>();
        for (Meter m : getMeterList()) {
            if (m.getResourceId().equals(resourceId)) {
                meters.add(m);
            }
        }
        return meters;
    }
}
